<template>
  <div class="comment">
    <div class="addcomment"
         v-show='!isFocus'>
      <input type="text"
             placeholder="写跟帖"
             @focus="handlerFocus" />
      <span class="comment"
            @click="$router.push({path: '/comment/'+article.id})">
        <i class="iconfont iconpinglun-"></i>
        <em>{{article.comment_length}}</em>
      </span>
      <!-- 文章收藏 -->
      <i class="iconfont iconshoucang"
         :class="{active:article.has_star}"
         @click='starArticle'></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment"
         v-show='isFocus'>
      <textarea ref='commtext'
                rows="5"
                v-model='content'></textarea>
      <div>
        <span @click="sendComment">发 送</span>
        <span @click='cancelComment'>取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { starThisArticle, publishComment } from '@/apis/post.js'
// import { inputfocus } from '@/utils/mydirectives.js'
export default {
  props: {
    article: {
      type: Object,
      required: true
    },
    // 这就是用户单击回复时的对应的评论对象
    comment: {
      type: Object,
      default: null
    }
  },
  watch: {
    // 只要单击回复，就会为comment赋值，只要赋值就会触发侦听器
    comment () {
      // 只有传递了评论对象，才弹出输入框
      if (this.comment) {
        this.isFocus = true
        setTimeout(() => {
          this.$refs.commtext.focus()
        }, 10);
        console.log('----', this.comment);
      }
    }
  },
  data () {
    return {
      // 文本输入框是否聚焦
      isFocus: false,
      content: ''
    }
  },
  methods: {
    // 取消评论
    cancelComment () {
      this.isFocus = false
      this.content = ''
      // 不能仅仅重置 isFocus,否则会造成第二次单击同一个回复按钮的时候不再触发侦听器，也就会造成输入框不再弹出
      // 不能在子组件中直接修改props成员的值，这个值只能由父组件进行修改
      this.$emit('reset')
    },
    // 文章的收藏 
    async starArticle () {
      let res = await starThisArticle(this.article.id)
      console.log(res);
      this.$toast.success(res.data.message)
      this.article.has_star = !this.article.has_star
    },
    //   获取焦点时触发
    handlerFocus () {
      this.isFocus = !this.isFocus
      // 让文本域聚焦
      setTimeout(() => {
        this.$refs.commtext.focus()
      }, 10);
    },
    // 发表评论
    async sendComment () {
      if (this.content.trim().length == 0) {
        this.$toast.fail('请输入评论的内容')
        return
      }
      let data = {
        content: this.content
      }
      // 如果是单击回复，就会有comment对象，说明有上一级评论
      if (this.comment) {
        data.parent_id = this.comment.id
      }
      let res = await publishComment(this.article.id, data)
      console.log(res);
      // 用户提示
      this.$toast.fail(res.data.message)
      // 清空内容
      this.content = ''
      // 让输入框隐藏
      this.isFocus = !this.isFocus
      // 页面刷新--子传父
      this.$emit('refreshData')
    }
  },
  // directives: {
  //   inputfocus
  // }
}
</script>

<style lang='less' scoped>
.comment {
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  background-color: #fff;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  .active {
    color: #f00;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
</style>
